//
//  SNSummaryInfoViewController.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/28.
//  Copyright © 2016年 stone. All rights reserved.
//


#import "SNFertilizerInformationController.h"
#import "SNFertilizerInformationCell.h"
@interface SNFertilizerInformationController ()<UITableViewDataSource,UITableViewDelegate>
/** tableView */
@property (nonatomic,weak) UITableView * tableView;
/** saveButton */
@property(weak, nonatomic) UIButton * saveButton;
@end

@implementation SNFertilizerInformationController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"肥料信息";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view, typically from a nib.
    [self addBackgroundView];
    [self addTableView];
    [self addBottomButton];
}
- (void)addBottomButton
{
    dispatch_async(dispatch_get_main_queue(), ^{
        
        CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:5]];
        
        CGRect rect = [self.tableView convertRect:rectInTableView toView:self.view];
        
        NSLog(@"rect CGRect:-%@", NSStringFromCGRect(rect));
        
        UIButton * saveButton = ({
            UIButton * button = [[UIButton alloc] init];
            
            [button setTitle:@"保存" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            
            [button setBackgroundImage:[SNUtils imageWithColor:HexRGB(0x67B245)] forState:UIControlStateNormal];
            [button setBackgroundImage:[SNUtils imageWithColor:HexRGB(0xAED39D)] forState:UIControlStateDisabled];
            
            [button addTarget:self action:@selector(saveButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
            
            CGFloat y = CGRectGetMaxY(rect)+16;
            
            button.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 140)*0.5 ,y, 140, 40);
            button.layer.cornerRadius = 4;
            button.layer.masksToBounds = YES;
            [self.view addSubview:button];
            
            
            
            button;
        });
        
        self.saveButton = saveButton;
        
    });
    
    
    
    
}
-(void)saveButtonClicked:(UIButton *)sender {
    NSLog(@"%s", __func__);
    
    for(int i = 0; i < 6; i++) {
        SNFertilizerInformationCell * cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:i]];
        
        switch (i) {
            case 0:
            {
                NSInteger fertTypes = 0;
                if ([cell.iconButton.currentTitle isEqualToString:@"单体肥"]) {
                    fertTypes = 1;
                }else if ([cell.iconButton.currentTitle isEqualToString:@"复合肥"]) {
                    fertTypes = 2;
                }else if ([cell.iconButton.currentTitle isEqualToString:@"掺混肥"]) {
                    fertTypes = 3;
                }
                NSLog(@"cell.iconButton.textField.text = %@",cell.iconButton.textField.text);
                self.productionPlanListFertlist.fertTypes = [NSString stringWithFormat:@"%ld",fertTypes];
            }
                break;
            case 1:
                self.productionPlanListFertlist.fertTotal = cell.rightTextField.text;
                break;
            case 2:
            {
                NSInteger index = 0;
                for(int i = 0; i < 4; i++) {
                   NSString * fertContentName =  [SNUtils getFertContentNameWithFertTypes:self.productionPlanListFertlist.fertTypes fertContent:[NSString stringWithFormat:@"%d",i]];
                    if ([fertContentName isEqualToString:cell.iconButton.currentTitle]) {
                        index = i;
                    }
                    
                }
                
                self.productionPlanListFertlist.fertContent = [NSString stringWithFormat:@"%ld",index];
            }
                break;
            case 3:
                self.productionPlanListFertlist.fertBrand = cell.rightTextField.text;
                break;
            case 4:
                self.productionPlanListFertlist.fertManu = cell.rightTextField.text;
                break;
            case 5:
                self.productionPlanListFertlist.fertPrice = cell.rightTextField.text;
                break;
            default:
                break;
        }
        
    }
    NSLog(@"%@",self.productionPlanListFertlist);
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)addBackgroundView
{
    //
    //
    //
    //        UIView * backgroundView = [[UIView alloc] init];
    //
    //    backgroundView.backgroundColor = kNormalBackgroundColor;
    //
    //
    //         [self.view addSubview:backgroundView];
    
    CALayer * layer = [CALayer layer];
    
    /** center & bounds */
    layer.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height -64);
    /** color */
    layer.backgroundColor = kNormalBackgroundColor.CGColor;
    
    [self.view.layer addSublayer:layer];
    
}
- (void)addTableView
{
    
    /** y坐标 */
    CGFloat y = 64;
    /** 高度 tableView */
    CGFloat height = self.view.bounds.size.height - y;
    /** 样式 tableView */
    UITableViewStyle tableViewStyle = UITableViewStyleGrouped;
    
    CGRect frame = CGRectMake(12, y, self.view.bounds.size.width-24, height);
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:frame style:tableViewStyle];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = kNormalBackgroundColor;
    tableView.scrollEnabled = NO;
    [self setupInit:tableView];
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //    tableView.separatorColor = [UIColor greenColor];
    [self.view addSubview:tableView];
    
    [self addObserver];
    [self addRequest];
}
#pragma mark - <addRequest>
- (void)addRequest {
    // addRequest
}
#pragma mark - <addObserver>
- (void)addObserver {
    // addObserver
}
- (void)dealloc {
    NSLog(@"%@ %s %@",[self class], __func__ , @"im dying");
}
/** 初始化 tableView */
- (void)setupInit:(UITableView *)tableView
{
    //MARK: - 设置tableHeaderView
    //    {
    //        UIView * tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    //        tableView.tableHeaderView = tableHeaderView;
    //    }
    
    //MARK: - 注册
    {
        /** xib 注册cell */
        registerForCellFromeNib(tableView,SNFertilizerInformationCell);
        /** class 注册cell */
        //        registerForCellFromeClass(tableView,UITableViewCell);
        
        /** xib 注册 headerSection */
        //    registerForHeaderFooterViewFromeNib(tableView,UITableViewHeaderFooterView);
        /** class 注册 headerSection */
        //    registerForHeaderFooterViewFromeClass(tableView,UITableViewHeaderFooterView);
        
        /** xib 注册 footerSection */
        //    registerForHeaderFooterViewFromeNib(tableView,UITableViewHeaderFooterView);
        /** class 注册 footerSection */
        //    registerForHeaderFooterViewFromeClass(tableView,UITableViewHeaderFooterView);
    }
    //MARK: - 设置高度
    {
        /** header */
        tableView.sectionHeaderHeight = 10;
        // tableView.estimatedSectionHeaderHeight = 100;
        // tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
        /** cell */
        tableView.estimatedRowHeight = 100;
        tableView.rowHeight = UITableViewAutomaticDimension;
        /** footer */
        tableView.sectionFooterHeight = 0.001;
        // tableView.estimatedSectionFooterHeight = 100;
        // tableView.sectionFooterHeight = UITableViewAutomaticDimension;
    }
}

#pragma mark - <UITableViewDataSource>
/** 组数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 6;
}
/** 行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
//MARK: -
//MARK: - cell
/** cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SNFertilizerInformationCell * cell = DequeueForCell(SNFertilizerInformationCell);
    //    SNTableViewCell * cell = [SNTableViewCell cellWithTableView:tableView];
    [cell configCellWithIndexPath:indexPath productionPlanListFertlist:self.productionPlanListFertlist];
    //    cell.contentView.backgroundColor = indexPath.row % 2 == 0 ? [UIColor colorWithRed:247 / 255.0 green:206 / 255.0 blue:166 / 255.0 alpha:1.0] : [UIColor colorWithRed:242 / 255.0 green:154 / 255.0 blue:76 / 255.0 alpha:1.0];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}
#pragma mark - <UITableViewDelegate>
/** 自定义section header */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UITableViewHeaderFooterView * headerView = DequeueForHeaderFooterView(UITableViewHeaderFooterView);
    
    // SNHeaderView * headerView = [SNHeaderView headerViewWithTableView:tableView];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.001;
}
/** 选中一行 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __func__);
    
    switch (indexPath.section) {
        case 0:// 肥料种类
            
            break;
        case 2:// 含量
            
            break;
        default:
            break;
    }
    
}
///** 取消 选中 */
//- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) {
//    NSLog(@"%s", __func__);
//}
/** 可编辑 */
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}
/** 删除样式 */
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return UITableViewCellEditingStyleDelete;
//}
/** 删除当前行 */
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//
//        [self.models removeObjectAtIndex:indexPath.row];
//
//        [tableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationLeft];
//}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@ | %s",self.class,__func__);
}
@end
